<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="操作人" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入操作人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进度" prop="stage">
        <el-select v-model="queryParams.stage" placeholder="请选择项目进度" clearable :style="{width: '100%'}">
          <el-option
            v-for="dict in dict.type.project_stage"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="历史操作" prop="operation">
        <el-select v-model="queryParams.operation" placeholder="请选择历史操作" clearable :style="{width: '100%'}">
          <el-option
            v-for="dict in dict.type.his_oper_info"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:history:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="操作人" align="center" prop="operator" />
      <el-table-column label="操作人角色" align="center" prop="rolename" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="操作类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.his_oper_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="stage" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_stage" :value="scope.row.stage"/>
        </template>
      </el-table-column>
      <el-table-column label="历史操作" align="center" prop="operation" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.his_oper_info" :value="scope.row.operation"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUndo(scope.row)"
          >撤回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listHistory, getHistory, delHistory, addHistory, updateHistory } from "@/api/system/history";

export default {
  name: "History",
  dicts: ['his_oper_type', 'his_oper_info', 'project_stage'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史记录表格数据
      historyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: this.$store.state.user.userId,
        operator: null,
        rolename: null,
        projectId: null,
        type: null,
        stage: null,
        state: null,
        operation: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    hisOperTypeFormatter(row, column) {
      return this.selectDictLabel(this.dict.type.his_oper_type, row.type);
    },
    hisOperInfoFormatter(row, column) {
      return this.selectDictLabel(this.dict.type.his_oper_info, row.operation);
    },
    projectStageFormatter(row, column) {
      return this.selectDictLabel(this.dict.type.project_stage, row.stage);
    },
    /** 查询历史记录列表 */
    getList() {
      this.loading = true;
      listHistory(this.queryParams).then(response => {
        this.historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        historyId: null,
        operator: null,
        rolename: null,
        projectId: null,
        type: null,
        stage: null,
        state: null,
        operation: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.historyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 撤销 */
    handleUndo(row) {
      this.$modal.confirm('确认要撤回该评审结果？').then(function() {
        return updateHistory(row);
      }).then(response => {
        this.form = response.data;
        this.$modal.msgSuccess("撤回成功！");
        this.getList();
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.historyId != null) {
            updateHistory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const historyIds = row.historyId || this.ids;
      this.$modal.confirm('是否确认删除历史记录编号为"' + historyIds + '"的数据项？').then(function() {
        return delHistory(historyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/history/export', {
        ...this.queryParams
      }, `history_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
